<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2017/2/13
 * Time: 14:52
 */
namespace Application\Controller;

use Application\Service\PriceService;
use Application\Utils\Page;
use Infobird\Controller\AbstractController;
use Infobird\Utils\LogUtils;

class PriceController extends AbstractController
{
    public function indexAction(){
        $price = new PriceService();
        try{
            $page = 1; //默认页数
            $pageSize = 10; //每页显示条数
            if ($this->getParamValue("page")){
                $page = $this->getParamValue("page"); //当前页数
            }else {
                $page = 1;
            }
            $total = $price->contPrice(); //总条数
            $total = isset($total[0]['COUNT(0)'])?$total[0]['COUNT(0)']:"0";
            $pageNum = ceil($total / $pageSize); //显示页数
            $result = $price->fetchAll($page,$pageSize);
            $pager = new Page();
            $url = $pager->createPage($page,$pageNum); //创建分页
            return array("result"=>$result,"page"=>$url);
        }catch (\Exception $e){
            LogUtils::log("t_price.log", 'save'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }

    /**
     * 功能：1、保存价格数据
     */
    public function saveAction(){
        $ycity = isset($_REQUEST['ycity'])?$_REQUEST['ycity']:"";
        $mcity = isset($_REQUEST['mcity'])?$_REQUEST['mcity']:"";
        $price = isset($_REQUEST['price'])?$_REQUEST['price']:"";
        $insert_dt = date("Y-m-d h:i:s");
        $arr = array(
            "oldadress" => $ycity,
            "destination" => $mcity,
            "price" => $price,
            "insert_dt" => $insert_dt
        );
        $priceService = new PriceService();
        try{
            $result = $priceService->save($arr);
            echo json_encode(array("result"=>"0","msg"=>"save ok"));
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_price.log", 'save'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }
}